package com.kgc.ljp.view;

import com.kgc.ljp.dao.impl.FlightDaoImpl;
import com.kgc.ljp.entity.Flight;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Scanner;

public class AirwayMgr {
    Scanner scan = new Scanner(System.in);
    String choose = null;
    FlightDaoImpl fdi = new FlightDaoImpl();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    static Map<String,Flight> flightMap = null;
    public void FirstMenu(){
        System.out.println("========欢迎使用国内航班机票查询服务系统=======");
        System.out.println("1.显示航班列表  2.新建航班信息  3.按城市名称模糊查询  0.退出");
        System.out.println("===============================================");
        do {
            choose = scan.next();
            if (!(choose.equals("1")||choose.equals("2")||choose.equals("3")||choose.equals("0"))){
                System.out.println("没有此选项,请重新输入");
            }else {
                break;
            }
        }while (true);
        switch (choose){
            case "1":
                flightMap = fdi.show();
                System.out.println("起飞城市\t到达城市\t日期\t\t价格\t\t折扣率");
                for (Flight flight:flightMap.values()) {
                    flight.print();
                }
                reBack();
                break;
            case "2":
                System.out.println("请输入编号:");
                String no = scan.next();
                System.out.println("请输入起飞城市:");
                String fCity = scan.next();
                System.out.println("请输入到达城市:");
                String dCity = scan.next();
                String time = null;
                System.out.println("请输入日期(yyyy-MM-dd):");
                time = scan.next();
                System.out.println("请输入全额票价:");
                double price = scan.nextDouble();
                System.out.println("请输入折扣率(%):");
                double disCount = scan.nextDouble();
                try {
                    if (fdi.addFlight(new Flight(no,fCity,dCity,sdf.parse(time),price,disCount))){
                        System.out.println("添加成功");
                    }else {
                        System.out.println("添加失败");
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                reBack();
                break;
            case "3":
                System.out.println("请输入您要查询的城市(可模糊查询):");
                String city = scan.next();
                System.out.println("起飞城市\t到达城市\t日期\t\t价格\t\t折扣率");
                flightMap = fdi.selectByCity(city);
//                if (flightMap!=null){
//
//                    for (Flight flight:flightMap.values()) {
//                        flight.print();
//                    }
//                }else{
//                    System.out.println("没有查询到相关信息");
//                }
                reBack();
                break;
            case "0":
                do {
                    System.out.println("是否确认退出（y/n）");
                    choose = scan.next();
                    if (choose.equalsIgnoreCase("y")){
                        System.out.println("谢谢使用");
                        break;
                    }else if (choose.equalsIgnoreCase("n")){
                        reBack();
                        break;
                    }
                }while (true);
                break;
        }
    }
    public void reBack(){
        System.out.println("任意键返回上一级");
        scan.next();
        FirstMenu();
    }
}
